package cx.v1.util;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.Signature;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class Sello {
    
    private String keyFile = null;
    
    public Sello(){
    }
    
    public byte[] getSello(byte[] info, PrivateKey priv, String algoritmo)throws Exception
    {

        try 
        {
            Security.addProvider(new BouncyCastleProvider());
            Signature sign = Signature.getInstance(algoritmo, "BC");
            
            sign.initSign(priv);
            
            sign.update(info, 0, info.length);
            
            return sign.sign();
        }
        catch (Exception ex) 
        {
            throw new Exception("Se presentó un error al intentar sellar los datos: " + ex.getMessage());
        }
    }
    
    public boolean verifySello(byte[] info, PublicKey pub, byte[] sello, String algoritmo) throws Exception
    {
        try
        {
            Signature sign = Signature.getInstance(algoritmo);
            
            sign.initVerify(pub);
            
            sign.update(info, 0, info.length);
            
            return sign.verify(sello);
        }
        catch(Exception ex)
        {
            throw new Exception("Se presentó un error al intentar verificar la firma de los datos introducidos: " + ex.getMessage());
        }
    }
    
}
